import sys
from collections import deque

import pygame

import basemaze

pygame.init()

FARBE_STAT_SCHIFFE = [0, 200, 0]
FARBE_TEXT = [255, 0, 0]
FARBE_HINTERGRUND = [100, 255, 100]
FARBE_AUSGEWAEHLT = [0, 0, 255]
FONT = pygame.font.Font(None , 30)

#Menu Fields
NUMBER = 1
TEXT = 2
RETURN = 3
MULTITEXT = 4

#Design Typs
LINES = 1
BRICKS = 2

#Maze Generators
PRIM = 1
DEPTH_FIRST_MOD = 2

class Menu:
    def __init__(self, caption):
        self.background = pygame.surface.Surface([1000, 1000])
        self.background.fill([240, 240, 240])
        
        self.screen = pygame.display.set_mode([600, 400])
        pygame.display.set_caption(caption)
        pygame.event.set_blocked(pygame.MOUSEMOTION)
        
        self.menu = [{"label": "Width:",
                      "text": basemaze.WIDTH,
                      "type": NUMBER,
                      "y_pos": 40},
                     
                     {"label": "Height:",
                      "text": basemaze.HEIGHT,
                      "type": NUMBER,
                      "y_pos": 80},
                     
                     {'label': "Algorithm:",
                      'text': deque([
                             {'label': "Prim", 
                              'action': "generate_maze_prim"},
                             {'label': "Depth First Search", 
                              'action': "generate_maze_dfs"},
                             {'label': "Prim (modificated)", 
                              'action': "generate_maze_prim_mod"},
                             {'label': "Depth First Search (modificated)", 
                              'action': "generate_maze_dfs_mod"}]),
                      'type': MULTITEXT,
                      'y_pos': 120},
                     
                     {'label': "Start Pos.:",
                      'text': deque([
                             {'label': "(0|0)",
                              'action': "[0, 0]"},
                             {'label': "Random",
                              'action': "basemaze.START_RANDOM"},
                             {'label': "Border",
                              'action': "basemaze.START_BORDER"},]),
                      'y_pos': 160,
                      'type': MULTITEXT},

                     {"label": "Design:",
                      "text": ["Line Design ->", "<- Brick Design"],
                      "y_pos": 200,
                      "type": TEXT},
                     
                     {"label": "Trackingline:",
                      "text": ["Yes ->", "<- No"],
                      "y_pos": 240,
                      "type": TEXT},
                    
                     {"text": "Start Game",
                      "y_pos": 280,
                      "type": RETURN,
                      "action": self.start_game},

                     {"text": "Exit",
                      "y_pos": 320,
                      "type": RETURN,
                      "action": self.exit},]
        self.selected = 6

    def draw_text(self, text, x_pos, y_pos, selected=False):
        if selected:
            farbe = FARBE_AUSGEWAEHLT
        else:
            farbe = FARBE_TEXT
        if isinstance(text, list):
            text = text[0]
        elif isinstance(text, deque):
            text = '<- ' + text[0]['label'] + ' ->'
        text = FONT.render(str(text), 1, farbe)
        self.screen.blit(text, [x_pos, y_pos])

    def draw_menu(self):
        self.screen.blit(self.background, (0, 0))
        for menupoint in self.menu:
            self.draw_text(menupoint["text"], 150, menupoint["y_pos"])
            if "label" in menupoint:
                self.draw_text(menupoint["label"], 20, menupoint["y_pos"])
        menupoint = self.menu[self.selected]
        self.draw_text(menupoint["text"], 150, menupoint["y_pos"], True)
        pygame.display.flip()

    def mainloop(self):
        pygame.display.init()
        self.draw_menu()
        pygame.key.set_repeat(100, 100)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        if not self.selected < 1:
                            self.selected -= 1
                        self.draw_menu()
                        
                    elif event.key == pygame.K_DOWN:
                        if not self.selected >= len(self.menu) - 1:
                            self.selected += 1
                        self.draw_menu()

                    elif event.key == pygame.K_RETURN:
                        if self.menu[self.selected]["type"] == RETURN:
                            self.menu[self.selected]["action"]()
                            return

                    elif event.key == pygame.K_RIGHT:
                        if self.menu[self.selected]["type"] == NUMBER \
                           and self.menu[self.selected]["text"] < 100:
                            self.menu[self.selected]["text"] += 1
                        elif self.menu[self.selected]["type"] == TEXT:
                            self.menu[self.selected]["text"].sort()
                        elif self.menu[self.selected]["type"] == MULTITEXT:
                            self.menu[self.selected]["text"].rotate(1)
                        self.draw_menu()

                    elif event.key == pygame.K_LEFT:
                        if self.menu[self.selected]["type"] == NUMBER \
                           and self.menu[self.selected]["text"] >= 1:
                            self.menu[self.selected]["text"] -= 1
                        elif self.menu[self.selected]["type"] == TEXT:
                            self.menu[self.selected]["text"].sort(reverse=True)
                        elif self.menu[self.selected]["type"] == MULTITEXT:
                            self.menu[self.selected]["text"].rotate(-1)
                        self.draw_menu()

                elif event.type == pygame.QUIT:
                    pygame.display.quit()
                    sys.exit()


    def start_game(self):
        width = self.menu[0]["text"]
        height = self.menu[1]["text"]

        if self.menu[4]["text"][0] == "Line Design ->": design = LINES
        else: design = BRICKS        
        
        algorithm = self.menu[2]["text"][0]["action"]
        start = self.menu[3]["text"][0]["action"]
            
        if self.menu[5]["text"][0] == "Yes ->": line = True
        else: line = False

        pygame.display.quit()
        
        self.selections = (width, height, design, line, algorithm, start)

    def exit(self):
        pygame.display.quit()
        pygame.quit()
        sys.exit()